package com.zhangdi.dwperms.controller;

import com.zhangdi.dwperms.request.LoginRequest;
import com.zhangdi.dwcommons.request.RegisterRequest;
import com.zhangdi.dwcommons.response.Response;
import com.zhangdi.dwcommons.response.ResponseUtil;
import com.zhangdi.dwperms.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class AccountController extends WarnController {

    @Autowired
    AccountService accountService;

    @PostMapping("/register")
    public Response register(@RequestBody RegisterRequest params) {
        return accountService.register(params);
    }

    @PostMapping("/login")
    public Response login(@RequestBody LoginRequest params, ServletResponse response) {
        return accountService.login(params, (HttpServletResponse)response);
    }

    @GetMapping("/logout")
    public Response logout(ServletRequest request) {
        accountService.logout((HttpServletRequest) request);

        return ResponseUtil.success();
    }
}
